# -*- coding:utf-8 -*-

## =====================================================================
## Copyright (C) 2007-2011  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## =====================================================================


from Tkinter import *
import sys, os
import calendar
import time
from tkMessageBox import *
from iVariable import Glob

class CalendarGUI(Toplevel):
	def __init__(self, parent=None, field=None, path=None, log=None):
		Toplevel.__init__(self, parent)
		self.parent=parent
		#~ Pmw.initialise(self.master)
		self.grab_set()
		self.transient(self.master)
		self.resizable(width=False, height=False)
		self.title(u"Calendrier")
		self.field=field
		self.monthList= Glob.MONTH_LIST
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Calendar-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Calendar-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tCalendarGUI\tStart")
		self.makeCalendar()

	# DEBUT de la gestion du module CALENDAR ------------------------------------------------------------------------------------------------------
	def initCal(self, year=2008, month=1):
		self.monthValue.set(self.monthList[month-1])
		self.yearValue.set(str(year))
		month=calendar.monthcalendar(year, month)
		count=0
		for week in month:
			for date in week:
				if date==0:self.daysListValue[count].set("")
				else:self.daysListValue[count].set(str(date))
				count+=1
		while count != 42:
			self.daysListValue[count].set("")
			count+=1

	def makeCalendar(self):
		yearFrame = Frame(self)
		self.prevYearCalBT=Button(yearFrame, text=u"<<", foreground=Glob.CAL_YEAR_COLOR, activebackground=Glob.CAL_MY_SELECT_COLOR , command=self.prevYearCal, relief=FLAT, pady=0)
		self.prevYearCalBT.pack(side=LEFT)
		self.yearValue=StringVar()
		self.yearCalLB=Label(yearFrame, width=10, foreground=Glob.CAL_YEAR_COLOR, textvariable=self.yearValue, pady=0)
		self.yearCalLB.pack(side=LEFT, fill=X)
		self.nextYearCalBT=Button(yearFrame, text=u">>", foreground=Glob.CAL_YEAR_COLOR, activebackground=Glob.CAL_MY_SELECT_COLOR , command=self.nextYearCal, relief=FLAT, pady=0)
		self.nextYearCalBT.pack(side=RIGHT)
		yearFrame.pack(fill=X)
		monthFrame = Frame(self)
		self.prevMonthCalBT=Button(monthFrame, text=u"  <", foreground=Glob.CAL_MONTH_FONT_COLOR, activebackground=Glob.CAL_MY_SELECT_COLOR , command=self.prevMonthCal, relief=FLAT, pady=0)
		self.prevMonthCalBT.pack(side=LEFT)
		self.monthValue=StringVar()
		self.monthCalLB=Label(monthFrame, width=10, foreground=Glob.CAL_MONTH_FONT_COLOR, textvariable=self.monthValue, pady=0)
		self.monthCalLB.pack(side=LEFT, fill=X)
		self.nextMonthCalBT=Button(monthFrame, text=u">  ", foreground=Glob.CAL_MONTH_FONT_COLOR, activebackground=Glob.CAL_MY_SELECT_COLOR , command=self.nextMonthCal, relief=FLAT, pady=0)
		self.nextMonthCalBT.pack(side=RIGHT)
		monthFrame.pack(fill=X)
		rows = (1, 2, 3, 4, 5, 6,7 )
		daysWeek=(u"L",u"M",u"M",u"J",u"V",u"S",u"D")
		self.daysListValue=(StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),
									StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),
									StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),
									StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),StringVar(),
									StringVar(),StringVar(),)
		count = 0
		for i in rows:
			s = Frame(self)
			if i == 1:
				for n in daysWeek:
					Button(s, text=n, command=self.selectDayOfTheWeek, relief=FLAT, height=1, width=1, padx=4, pady=1, activebackground=Glob.CAL_WEEK_BKG_COLOR, background=Glob.CAL_WEEK_BKG_COLOR).pack(side=LEFT)
			else:
				for n in daysWeek:
					Button(s, textvariable=self.daysListValue[count], command=lambda x=count:self.selectDay(x), relief=FLAT, height=1, width=1, padx=4, pady=1, activeforeground=Glob.CAL_SELECT_COLOR, activebackground=Glob.CAL_MY_SELECT_COLOR ).pack(side=LEFT)
					count+=1
			s.pack()
		self.selectTodayCalBT=Button(self, text=u"Aujourd'hui", activeforeground=Glob.CAL_SELECT_COLOR, activebackground=Glob.CAL_MY_SELECT_COLOR , command=self.selectToday, relief=FLAT, pady=0)
		self.selectTodayCalBT.pack(fill=X)
		# Afficher le mois en cours
		t=time.strftime("%d/%m/%Y" ,time.localtime())
		today=t.split("/")
		self.initCal(year=int(today[2]), month=int(today[1]))

	def prevYearCal(self, event=None):
		count=0
		for m in self.monthList:
			if m==self.monthValue.get():mth=count+1
			count+=1
		self.initCal(year=int(self.yearValue.get())-1, month=mth)

	def nextYearCal(self, event=None):
		count=0
		for m in self.monthList:
			if m==self.monthValue.get():mth=count+1
			count+=1
		self.initCal(year=int(self.yearValue.get())+1, month=mth)

	def prevMonthCal(self, event=None):
		count=0
		for m in self.monthList:
			if m==self.monthValue.get():mth=count+1
			count+=1
		mth-=1
		if mth==0:mth=12
		self.initCal(year=int(self.yearValue.get()), month=mth)

	def nextMonthCal(self, event=None):
		count=0
		for m in self.monthList:
			if m==self.monthValue.get():mth=count+1
			count+=1
		mth+=1
		if mth==13:mth=1
		self.initCal(year=int(self.yearValue.get()), month=mth)

	def selectDay(self, value):
		if self.daysListValue[value].get() != "":
			count=0
			for month in self.monthList:
				if month == self.monthValue.get():selectedMonth = count+1
				count+=1
			if selectedMonth < 10:strMonth="0"+str(selectedMonth)
			else:strMonth=str(selectedMonth)
			if int(self.daysListValue[value].get()) < 10:strDay="0"+self.daysListValue[value].get()
			else:strDay=self.daysListValue[value].get()
			self.field.set(strDay+"/"+strMonth+"/"+self.yearValue.get())
			self.destroy()

	def selectDayOfTheWeek(self):
		pass

	def selectToday(self):
		self.field.set(time.strftime("%d/%m/%Y" ,time.localtime()))
		self.destroy()
	# FIN de la gestion du module CALENDAR ---------------------------------------------------------------------------------------------------------